package com.itheima.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ProductAttributeCategoryMapper;
import com.itheima.mall.mapper.ProductAttributeMapper;
import com.itheima.mall.pojo.ProductAttribute;
import com.itheima.mall.pojo.ProductAttributeCategory;
import com.itheima.mall.service.PmsProductAttributeCategoryService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService {

   @Autowired
   private ProductAttributeCategoryMapper productAttributeCategoryMapper;

   @Autowired
   private ProductAttributeMapper productAttributeMapper;

   //添加商品属性分类
    @Override
    public void createProductAttribute(String name) {
        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        productAttributeCategory.setName(name);
        productAttributeCategoryMapper.insert(productAttributeCategory);
    }

    //分页查询商品属性
    @Override
    public PageResult productAttributeList(Integer pageNum, Integer pageSize) {
        Page<ProductAttributeCategory> productAttributePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductAttributeCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<ProductAttributeCategory> pageInfo = productAttributeCategoryMapper.selectPage(productAttributePage, lambdaQueryWrapper);
        return new PageResult(pageNum , pageSize , (int)pageInfo.getTotal() ,pageInfo.getRecords());
    }

    /**
     * 通过id删除商品属性
     * @param id
     */
    @Override
    public void deleteProductAttributeById(Integer id) {
        productAttributeCategoryMapper.deleteById(id);

    }
    //获取所有商品属性分类及其下属性
    @Override
    public List<ProductAttributeCategory> listWithAttr() {
        //查询所有的属性分类
        LambdaQueryWrapper<ProductAttributeCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<ProductAttributeCategory> categoryList = productAttributeCategoryMapper.selectList(lambdaQueryWrapper);
        //根据id其下属性
        for (ProductAttributeCategory productAttributeCategory : categoryList) {
            Long id = productAttributeCategory.getId();
            LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId ,id);
            List<ProductAttribute> list = productAttributeMapper.selectList(queryWrapper);
            productAttributeCategory.setProductAttributeList(list);

        }
        return categoryList;
    }

    //修改商品属性分类
    @Override
    public void updateProductAttributeCategoryById(Integer id, String name) {
        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        productAttributeCategory.setName(name);
        //通过id修改商品属性分类
        LambdaQueryWrapper<ProductAttributeCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductAttributeCategory::getId , id);
        productAttributeCategoryMapper.update(productAttributeCategory , lambdaQueryWrapper);

    }

    //获取单个商品属性信息
    @Override
    public ProductAttributeCategory selectById(Integer id) {
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(id);
        return productAttributeCategory;

    }

}
